package zwp.exercise.intereview.base.sort;

import java.util.Arrays;

/**
 * @Author zwp14
 * @create 2024/6/28 11:01
 * @description 选择排序复习1
 */
public class SelectionSortReview1 {
    public static void main(String[] args) {
        //定义数组
        int[] array = {5, 9, 7, 4, 1, 3, 2, 8};
        //最多交换轮次7次
        for (int i = 0; i < array.length - 1; i++) {
            //假设最小数索引,由此划分有序集和无序集
            int idxMin = i;
            //<=idxMin的是有序集,>idxMin的是无序集,从无序集里面拿数和有序集里面的数进行比较然后排序
            for (int j = idxMin + 1; j < array.length; j++) {
                if (array[idxMin] > array[j]){
                    idxMin = j;
                }
            }
            //如果发现无序集里面存在比有序集小的数字则进行交换排序
            if (idxMin != i){
                int temp = array[idxMin];
                array[idxMin] = array[i];
                array[i] = temp;
            }
            System.out.println(Arrays.toString(array));
        }
    }
}
